package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.GameSe;
import com.cfets.annualAssessment.entity.Gamesit;
import com.cfets.annualAssessment.mapper.GameScoreMapper;
import com.cfets.annualAssessment.mapper.GameSitMapper;
import com.cfets.annualAssessment.util.CheckUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by ShenYiChou on 2017/9/5.
 */
@Service
public class GameScoreService {
    @Autowired
    private GameScoreMapper gameScoreMapper;
    @Autowired
    private GameSitMapper gameSitMapper;


    public boolean newGameSe(GameSe gameSe) {
        return gameScoreMapper.insertSelective(gameSe)>0;
    }

    public boolean updateGameSe(GameSe gameSe) {
        return gameScoreMapper.updateByPrimaryKey(gameSe)>0;
    }

    public boolean newGameSit(Gamesit gamesit) {
        return gameSitMapper.insertSelective(gamesit)>0;
    }

    public List<GameSe> getGamePage(GameSe gameSe) {
        Page<?> page = PageHelper.startPage(gameSe.getPage(), gameSe.getRows());
        List<GameSe> enMkts = this.getGame(gameSe);
        gameSe.setTotalCount(page.getTotal());
        return enMkts;
    }

    public List<GameSe> getGame(GameSe gameSe) {
        Example example=new Example(GameSe.class);
        Example.Criteria criteria =example.createCriteria();
        if(!CheckUtil.isEmpty(gameSe.getIs_sign())){
            criteria.andEqualTo("is_sign",gameSe.getIs_sign());
        }
        if(!StringUtils.isEmpty(gameSe.getName())){
            criteria.andLike("name","%"+gameSe.getName()+"%");
        }
        if(!StringUtils.isEmpty(gameSe.getOrg_name())){
            criteria.andLike("org_name","%"+gameSe.getOrg_name()+"%");
        }
        return gameScoreMapper.selectByExample(example);
    }

//    public List<Gamesit> getSit(Gamesit gamesit) {
//        Example example=new Example(Gamesit.class);
//        Example.Criteria criteria =example.createCriteria();
//        if(!StringUtils.isEmpty(gamesit.getOrg_name())){
//            criteria.andEqualTo("org_name",gamesit.getOrg_name());
//        }
//        return gameSitMapper.selectByExample(example);
//    }

    public List<GameSe> getLogin(GameSe gameSe) {
        Example example=new Example(GameSe.class);
        Example.Criteria criteria =example.createCriteria();
        if(!CheckUtil.isEmpty(gameSe.getTel())){
            criteria.andEqualTo("tel",gameSe.getTel());
        }
        if(!StringUtils.isEmpty(gameSe.getName())){
            criteria.andEqualTo("name",gameSe.getName());
        }
//        criteria.andEqualTo("is_sign",0);
        return gameScoreMapper.selectByExample(example);
    }
    public List<GameSe> getVIPLogin(GameSe gameSe) {
        Example example=new Example(GameSe.class);
        Example.Criteria criteria =example.createCriteria();
        if (!StringUtils.isEmpty(gameSe.getOrg_name())) {
            criteria.andEqualTo("org_name",gameSe.getOrg_name());
        }
        if(!StringUtils.isEmpty(gameSe.getName())){
            criteria.andEqualTo("name",gameSe.getName());
        }
//        criteria.andEqualTo("is_sign",0);
        return gameScoreMapper.selectByExample(example);
        gameScoreMapper.up
    }

    //返回受邀的用户
//    public List<GameSe> LoginOn(GameSe gameSe) {
//        Example example=new Example(GameSe.class);
//        Example.Criteria criteria =example.createCriteria();
//        if (!CheckUtil.isEmpty(gameSe.getOrg_name())){
//            criteria.andEqualTo("org_name",gameSe.getOrg_name());
//        }
//        if(!StringUtils.isEmpty(gameSe.getName())){
//            criteria.andEqualTo("name",gameSe.getName());
//        }
//        return gameScoreMapper.selectByExample(example);
//    }

    public List<GameSe> getAllGameSe(){
        return gameScoreMapper.selectAll();
    }

    public boolean deleteGameSeById(int id) {
        return gameScoreMapper.deleteByPrimaryKey(id)>0;
    }

    public boolean deleteAllGameSe() {
        Example example=new Example(GameSe.class);
        return gameScoreMapper.deleteByExample(example)>0;
    }
    public boolean deleteAllGameSit() {
        Example example=new Example(Gamesit.class);
        return gameSitMapper.deleteByExample(example)>0;
    }
    public boolean HaveSign (GameSe gameSe) {
        gameSe.setIs_sign(1);
        return gameScoreMapper.updateByPrimaryKey(gameSe)>0;
    }

    public GameSe getGameByToken (String token){
        Example example=new Example(Gamesit.class);
        Example.Criteria criteria =example.createCriteria();
        GameSe gameSe = new GameSe();
        if(!StringUtils.isEmpty(token)){
            criteria.andEqualTo("org_name",token);
        }
        List<Gamesit> gamesitList= gameSitMapper.selectByExample(example);
        if(gamesitList.isEmpty()||gamesitList.size()>1){
            return gameSe;
        }
        return gameScoreMapper.selectByPrimaryKey(gamesitList.get(0).getSit_x());
    }
}
